package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heatup.apt.api.service.UserService;
import com.heatup.apt.common.constants.UserConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;

/**
 * 预售会员控制器
 * @author jixiangyun
 * 2016年10月31日 下午12:30:38
 */
@Controller
@RequestMapping("/presell_vip")
public class PresellVipController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
    @RequestMapping(value = "/check", method = {RequestMethod.POST, RequestMethod.GET})
	public String add_presell_vip(HttpServletRequest request,HttpServletResponse response){
        Integer userid = this.getUserId();
        String callback=RequestTools.RequestString(request, "callback", "");
        String checkCode = RequestTools.RequestString(request, "checkCode", "");    
        String mobiles = RequestTools.RequestString(request, "mobile", "");  
        ApiResultModel<Map<String,Object>> apiResultModel=new ApiResultModel<Map<String,Object>>();
        apiResultModel.setLogined(this.isLogined());
        Map<String,Object> map = new HashMap<String,Object>();
        
        //未登陆
        if(userid == null){
        	apiResultModel.setRet(ApiResultModel.RET_ERROR);
        	apiResultModel.setMsg("用户未登陆");
        	return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
        }
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("userId", userid);
        maps.put("mobile", mobiles);
        
        try{
        	//有效期验证
        	Object syscode = this.getSession(SESSION_KEY_CHECK_CODE);
        	Object lastSend = this.getSession(SESSION_KEY_CHECK_CODE_LAST_SEND);
        	Object mobile = this.getSession(SESSION_KEY_MOBILE);
        	if(syscode==null || lastSend==null){
        		apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setMsg("无验证码");
                map.put("isCode", false);
                apiResultModel.setData(map);
                return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
        	}
        	
    		long lastSendTime = (long)lastSend;
    		long currentTime = System.currentTimeMillis();
    		long seconds =  (currentTime - lastSendTime)/1000;
    		if(seconds > UserConstants.CHECK_CODE_TIMEOUT){
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setMsg("验证码已失效,请重新发送");
                map.put("checkCode", false);
                map.put("isCode", true);
                apiResultModel.setData(map);
                return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
    		}
    		
    		//验证码验证
        	if(syscode!=null && syscode.toString().equals(checkCode)){
        		maps.put("userId", userid);
                maps.put("mobile", mobiles);
                UserInfo  userInfo = userService.updatePresellUser(maps);
                apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
                apiResultModel.setMsg("验证成功");
                // 不是预售会员
                if(userInfo == null){
                	apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
                    apiResultModel.setMsg("不是预售会员");
                    map.put("isprese", false);
                    map.put("checkCode", true);
                    map.put("isCode", true);
                }else{ 
                	apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
                    apiResultModel.setMsg("是预售会员");
                    map.put("isprese", true);
                    map.put("checkCode", true);
                    map.put("isCode", true);
                    map.put("startTime", userInfo.getVipStartTime());
                }
        	}else{
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setMsg("验证码错误");
                map.put("checkCode", false);
                map.put("isCode", true);
        	}
        }catch(Exception ex){
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
            apiResultModel.setMsg("系统错误" + ex.getMessage());
        }
        apiResultModel.setData(map);
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
	}
	
	@ResponseBody
    @RequestMapping(value = "/getUser", method = {RequestMethod.POST, RequestMethod.GET})
	public String get_user(HttpServletRequest request,HttpServletResponse response){
		Integer userid = this.getUserId();
        String callback=RequestTools.RequestString(request, "callback", "");
        ApiResultModel<Map<String,Object>> apiResultModel=new ApiResultModel<Map<String,Object>>();
        apiResultModel.setLogined(this.isLogined());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("level", this.getUserInfo().getLevel());
        map.put("user",this.getUserInfo());
        apiResultModel.setData(map);
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
	}
	
}
